/**
 * pagination 
 * 
 * @author blackstar
 * @email abc@123.com
 * 
 * how to use？
 * add pagination html
 * add this js
 * invoke paginationApp.init method
 * invoke  paginationApp $on like this:
 * paginationApp.$on('getPages',function(pageNum){
						console.info('logic write here');
						});
 */
/**
 * pagination
 */

var paginationApp = new Vue({
	el:"#pagination",
	data:{
		current:1,
		total:1,
		showPages:[{num:1,active:true}],
		defaultShowCount:5,
		count:0
	},
	methods:{
		init:function(count,pageCount){
			//vue remove getPages function
			this.$off("getPages");
			this.count = count;
			if(!this.count){
				this.count = 0;
			}
			var totalPage = Math.ceil(count/pageCount);
			if(!totalPage){
				totalPage = 1;
			}
			this.total = totalPage;
			this.current=1;
			this.showPage= [];
			var showPageCount = 5;
			if(totalPage<this.defaultShowCount){
				showPageCount=totalPage;
			}
			this.showPages=[];
			for(var i = 0 ;i<showPageCount;i++){	
				var isActive=false;
				if(i == 0){
					isActive=true;
				}
				this.showPages.push({num:i+1,active:isActive});
			}
			$(".jumpPage").val(this.current);
			//默认显示第一页
		},
		changeStyle:function(pageNum){
			//判断总数是否超过5页
			if(this.total >this.defaultShowCount){
				//数字差
				var numSpan  = parseInt(this.defaultShowCount/2);
				var firstPageNum = pageNum-numSpan;
				var lastPageNum  = pageNum+numSpan;
				//如果最后页大于总页数
				if(lastPageNum>this.total){					
					firstPageNum=firstPageNum-lastPageNum+this.total;
					lastPageNum = this.total;
				}
				if(firstPageNum <= 1){
					lastPageNum = lastPageNum-firstPageNum+1;
					firstPageNum = 1;
				}
				var newShowPages = [];
				for(var i = 0;i<this.defaultShowCount;i++){
					newShowPages.push({num:firstPageNum+i,active:false});
				}
				this.showPages = newShowPages;
				
			}
			//add style
			for(var i =0 ;i<this.showPages.length;i++){
				if(this.showPages[i].num == pageNum){
					this.showPages[i].active=true;
				}else{
					this.showPages[i].active=false;
				}
				
			}
		},
		showByPageNum:function(pageNum){
			this.$emit("getPages",pageNum);
		},
		//显示内容
		show:function(obj){
			//add active
			var num = parseInt($(obj).text());
			$(".jumpPage").val(num);
			this.changeStyle(num);
			this.current =num;
			this.showByPageNum(num);
			
		},
		
		previous:function(){
			if(this.current >1){
				this.current = this.current-1;
				this.changeStyle(this.current);
				$(".jumpPage").val(this.current);
			}
			//TODO changeshowpages
			this.showByPageNum(this.current);
		},
		next:function(){
			if(this.current <this.total ){
				this.current = this.current+1;
				this.changeStyle(this.current);
				$(".jumpPage").val(this.current);
			}
			this.showByPageNum(this.current);
		},
		first:function(){
			this.current = 1;
			this.changeStyle(this.current);
			this.showByPageNum(this.current);
			$(".jumpPage").val(this.current);
		},
		last:function(){
			this.current = this.total;
			this.changeStyle(this.current);
			this.showByPageNum(this.current);
			$(".jumpPage").val(this.current);
		},
		jumpPage:function(total){	
			var jumpPage = parseInt($(".jumpPage").val());
			this.changeStyle(jumpPage);
			this.current =jumpPage;
			this.showByPageNum(jumpPage);
		}
	}
});
